import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'
Vue.use(VueRouter)
const routes =[
    {
        path: '/login',
        name: 'login',
        component: () => import('../login/login')
    },
]
const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach( (to, from, next) => {
    // start progress bar
    // determine whether the user has logged in
    const hasToken = localStorage.getItem("token")
    // const hasToken = getToken()
    if (hasToken) {
        if (to.path === '/login') {
            // if is logged in, redirect to the home page
            next({ path: '/index' })
        } else {
            if(to.path==='/'){
                next({ path: '/index' })
            }else{
                next()
            }

        }
    } else {
        if(to.path==='/login'){
            next()
        }else{
            next('/login')
        }

    }
})
export default router
